package com.winderinfo.yikaotianxia.core.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.ButterKnife;
import butterknife.Unbinder;

public abstract class BaseFragment extends Fragment {

    public Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), initLayoutId(), null);
        unbinder = ButterKnife.bind(this, view);
        initView(view);
        initData();
        return view;
    }

    /**
     * 加载页面
     */
    protected abstract int initLayoutId();

    /**
     * 加载布局
     */
    protected abstract void initView(View view);

    /**
     * 加载数据
     */
    protected abstract void initData();

    /**
     * 跳转至另一个Activity
     *
     * @param targetActivityClass 目标Activity
     */
    protected void openActivity(Class<?> targetActivityClass) {
        Intent intent = new Intent(getActivity(), targetActivityClass);
        startActivity(intent);
    }

    /**
     * 跳转至另一个Activity
     *
     * @param targetActivityClass 目标Activity
     * @param bundle              传递参数
     */
    protected void openActivity(Class<?> targetActivityClass, Bundle bundle) {
        Intent intent = new Intent(getActivity(), targetActivityClass);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    /**
     * 跳转至另一个Activity并且关闭当前Activity
     *
     * @param targetActivityClass 目标Activity
     */
    protected void openActivityAndCloseThis(Class<?> targetActivityClass) {
        openActivity(targetActivityClass);
        getActivity().finish();
    }

    /**
     * 跳转至另一个Activity并且关闭当前Activity
     *
     * @param targetActivityClass 目标Activity
     * @param bundle              传递参数
     */
    protected void openActivityAndCloseThis(Class<?> targetActivityClass, Bundle bundle) {
        openActivity(targetActivityClass, bundle);
        getActivity().finish();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


}
